package cn.tarena.ht.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.tarena.ht.pojo.Dept;
import cn.tarena.ht.service.DeptService;
import cn.tarena.ht.tool.IdCheck;

@Controller
@RequestMapping("/sysadmin/dept")
public class DeptController {
	@Autowired
	private DeptService deptService;
	@RequestMapping("/list")
	public String findAll(Model model) {
		List<Dept> deptList = deptService.findAll();
		model.addAttribute("deptList", deptList);
		return "/sysadmin/dept/jDeptList";
	}
	@RequestMapping("/delete")
	public String toDelete(@RequestParam(value="deptId",required=false)String[] deptIds,HttpSession session) {
		boolean result = IdCheck.checkIds(deptIds, session);
		if(result) {
			deptService.toDelete(deptIds);
		}
		return "redirect:/sysadmin/dept/list";
	}
	@RequestMapping("/start")
	public String stateToStart(@RequestParam(value="deptId",required=false)String[] deptIds,HttpSession session) {
		int state = 1;
		boolean result = IdCheck.checkIds(deptIds, session);
		if(result) {
			deptService.updateState(deptIds,state);
		}
		return "redirect:/sysadmin/dept/list";
	}
	@RequestMapping("/stop")
	public String stateToStop(@RequestParam(value="deptId",required=false)String[] deptIds,HttpSession session) {
		int state = 0;
		boolean result = IdCheck.checkIds(deptIds, session);
		if(result) {
			deptService.updateState(deptIds,state);
		}
		return "redirect:/sysadmin/dept/list";
	}
	@RequestMapping("/tocreate")
	public String toCreate(Model model) {
		List<Dept> parentList = deptService.findAll();
		model.addAttribute("parentList", parentList);
		return "/sysadmin/dept/jDeptCreate";
	}
	@RequestMapping("/save")
	public String save(Dept dept) {
		deptService.saveDept(dept);
		return "redirect:/sysadmin/dept/list";
	}
	@RequestMapping("/toupdate")
	public String toUpdate(@RequestParam(value="deptId",required=false)String[] deptIds,HttpSession session) {
		boolean result = IdCheck.checkId(deptIds, session);
		if(result) {
			Dept dept = deptService.findDeptById(deptIds[0]);
			List<Dept> parentList = deptService.findParentListById(deptIds[0]);
			session.setAttribute("dept", dept);
			session.setAttribute("parentList", parentList);
			return "/sysadmin/dept/jDeptUpdate";
		}else {
			return "redirect:/sysadmin/dept/list";
		}
	}
	@RequestMapping("/update")
	public String updateDept(Dept dept) {
		deptService.updateDept(dept);
		return "redirect:/sysadmin/dept/list";
	}
	@RequestMapping("/toview")
	public String toView(@RequestParam(value="deptId",required=false)String[] deptIds,HttpSession session) {
		boolean result = IdCheck.checkId(deptIds, session);
		if(result) {
			Dept dept = deptService.findDeptById(deptIds[0]);
			session.setAttribute("dept", dept);
			return "/sysadmin/dept/jDeptView";
		}else {
			return "redirect:/sysadmin/dept/list";
		}
	}
}
